package com.skivingcloud.report.mng.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.report.mng.entity.Report;
import com.skivingcloud.report.mng.service.IReportService;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 大屏报表 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-28
 */
@RestController
@RequestMapping("/reportMng/report")
public class ReportController {
	private static Logger log = LoggerFactory.getLogger(ReportController.class);

	@Autowired
	private IReportService reportService;
	
	/**
	 * 分页
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = reportService.queryPage(params);
		return R.ok().put("page", pages);
	}

	/**
	 * 根据主键查询
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		Report report = reportService.selectById(id);
		return R.ok().put("data", report);
	}

	/**
	 * 新增
	 * 
	 * @param report
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "save", description = "新增报表数据")
	@PostMapping("/save")
	public R save(@RequestBody Report report) {
		try {
			reportService.add(report, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存报表数据失败!", e);
			return R.error("保存报表数据失败!");
		}
	}

	/**
	 * 修改
	 * 
	 * @param report
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "update", description = "修改报表数据")
	@PostMapping("/update")
	public R update(@RequestBody Report report) {
		try {
			reportService.update(report, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存报表数据失败!", e);
			return R.error("保存报表数据失败!");
		}
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "delete", description = "删除报表数据")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			reportService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除报表数据失败!", e);
			return R.error("删除报表数据失败!");
		}
	}
}
